package com.iplant.basic.serviceImpl.dao.bfc;

import com.iplant.base.po.BaseUser;
import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.DBCutTableModes;
import com.iplant.base.po.enums.DBSchemas;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.annotation.dao.DBBaseDAO;
import com.iplant.base.utils.general.CalendarTool;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.basicenum.bfc.BFCMessageStatus;
import com.iplant.basic.service.basicenum.bfc.BFCMessageType;
import com.iplant.basic.service.po.bfc.BFCMessage;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.serviceImpl.BFCServiceImpl;
import com.iplant.basic.serviceImpl.dao.bms.BMSEmployeeDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;

@DBBaseDAO(BFCMessage.class)
public class BFCMessageDAO extends BaseDAO {


    private static Logger logger = LoggerFactory.getLogger(BFCMessageDAO.class);

    private static BFCMessageDAO Instance;


    private List<BFCMessage> BFC_GetMessageList(BMSEmployee wLoginUser, int wResponsorID, int wStationID,   int wType,
                                                List<Integer> wModuleID, List<Integer> wMessageIDList, List<Integer> wActive, int wSendStatus, int wShiftID,
                                                Calendar wStartTime, Calendar wEndTime, int wStepID, Pagination wPagination,
                                                OutResult<Integer> wErrorCode) {

        Map<String, Object> wParamMap = new HashMap<String, Object>();

        wParamMap.put("CompanyID", -1);
        wParamMap.put("ResponsorID", wResponsorID);
        wParamMap.put("StationID", wStationID);
        wParamMap.put("StepID", wStepID);
        wParamMap.put("Type", wType);
        wParamMap.put("ModuleID", StringUtils.Join(",", wModuleID));
        wParamMap.put("MessageID", StringUtils.Join(",", wMessageIDList));
        wParamMap.put("Active", StringUtils.Join(",", wActive));
        wParamMap.put("ShiftID", wShiftID);
        wParamMap.put("SendStatus", wSendStatus);
        wParamMap.put("StartTime", wStartTime);
        wParamMap.put("EndTime", wEndTime);


        return this.SelectPOList(wLoginUser, wParamMap, wPagination, wErrorCode);

    }


//    public List<BFCMessage> BFC_GetMessageList(
//            BMSEmployee wLoginUser, int wResponsorID, int wStationID, String wStationNo, int wType,
//            List<Integer> wModuleID, List<Integer> wMessageIDList, List<Integer> wActive, int wSendStatus, int wShiftID,
//            Calendar wStartTime, Calendar wEndTime, int wStepID, Pagination wPagination,
//            OutResult<Integer> wErrorCode) {
//        List<BFCMessage> wResult = new ArrayList<BFCMessage>();
//
//        try {
//
//            if (wStationNo == null) {
//                wStationNo = "";
//            }
//
//            Calendar wBaseTime = CalendarTool.getCalendar(2000, 1, 1);
//            if (wStartTime.compareTo(wBaseTime) < 0) {
//                wStartTime = wBaseTime;
//            }
//            if (wEndTime.compareTo(wBaseTime) < 0) {
//                wEndTime = wBaseTime;
//            }
//            if (wStartTime.compareTo(wEndTime) > 0) {
//                return wResult;
//            }
//            if (wMessageIDList == null) {
//                wMessageIDList = new ArrayList<Integer>();
//            }
//            if (wActive == null) {
//                wActive = new ArrayList<Integer>();
//            }
//            if (wModuleID == null) {
//                wModuleID = new ArrayList<Integer>();
//            }
//            wMessageIDList.removeIf(p -> p <= 0);
//
//            wActive.removeIf(p -> p < 0);
//            wModuleID.removeIf(p -> p <= 0);
//
////            String wSQL = StringUtils.Format("SELECT  t.* FROM {0}.bfc_message t WHERE 1=1 " +
////                            " and ( :wCompanyID<= 0 or  t.CompanyID  =:wCompanyID)   " +
////                            " and ( :wResponsorID<= 0 or  t.ResponsorID  =:wResponsorID)   " +
////                            " and ( :wStationID<= 0 or  t.StationID  =:wStationID)   " +
////                            " and ( :wStationNo ='''' or  t.StationNo  =  :wStationNo )  " +
////                            " and ( :wType<= 0 or  t.Type  =:wType)   " +
////                            " and ( :wModuleID ='''' or  t.ModuleID IN ({3}))   " +
////                            " and ( :wMessageID ='''' or  t.MessageID  IN ({1}))   " +
////                            " and ( :wActive ='''' or  t.Active  IN ({2}) )   " +
////                            " and ( :wSendStatus<= 0 or (:wSendStatus & t.SendStatus) = 0 )   " +
////                            " and ( :wShiftID <= 0 or  t.ShiftID   = :wShiftID)   " +
////                            " and ( :wStepID <= 0 or  t.StepID   = :wStepID)   " +
////                            " and ( :wStartTime <=str_to_date(''2010-01-01'', ''%Y-%m-%d'')  or :wStartTime <=  t" +
////                            ".EditTime )" +
////                            " and ( :wEndTime <=str_to_date(''2010-01-01'', ''%Y-%m-%d'')  or :wEndTime >=  t" +
////                            ".CreateTime ) ",
////                    super.getDBName(),
////                    wMessageIDList.size() > 0 ? StringUtils.Join(",", wMessageIDList) : "0",
////                    wActive.size() > 0 ? StringUtils.Join(",", wActive) : "0",
////                    wModuleID.size() > 0 ? StringUtils.Join(",", wModuleID) : "0");
//
//            Map<String, Object> wParamMap = new HashMap<String, Object>();
//
//            wParamMap.put("CompanyID", -1);
//            wParamMap.put("ResponsorID", wResponsorID);
//            wParamMap.put("StationNo", wStationNo);
//            wParamMap.put("StationID", wStationID);
//            wParamMap.put("StepID", wStepID);
//            wParamMap.put("Type", wType);
//            wParamMap.put("ModuleID", StringUtils.Join(",", wModuleID));
//            wParamMap.put("MessageID", StringUtils.Join(",", wMessageIDList));
//            wParamMap.put("Active", StringUtils.Join(",", wActive));
//            wParamMap.put("ShiftID", wShiftID);
//            wParamMap.put("SendStatus", wSendStatus);
//            wParamMap.put("StartTime", wStartTime);
//            wParamMap.put("EndTime", wEndTime);
//
//
//            wResult = this.SelectPOList(wLoginUser,wParamMap,wPagination,wErrorCode);
//
////            BFCMessage wBFCMessage = null;
////
////            List<Map<String, Object>> wQueryResult = super.queryForList(wSQL, wParamMap, wPagination);
////            // wReader\[\"(\w+)\"\]
////            for (Map<String, Object> wReader : wQueryResult) {
////
////                wBFCMessage = new BFCMessage();
////                wBFCMessage.ID = StringUtils.parseInt(wReader.get("ID"));
////                wBFCMessage.CreateTime = StringUtils.parseCalendar(wReader.get("CreateTime"));
////                wBFCMessage.CompanyID = StringUtils.parseInt(wReader.get("CompanyID"));
////                wBFCMessage.EditTime = StringUtils.parseCalendar(wReader.get("EditTime"));
////                wBFCMessage.MessageID = StringUtils.parseInt(wReader.get("MessageID"));
////                wBFCMessage.MessageText = StringUtils.parseString(wReader.get("MessageText"));
////                wBFCMessage.StationID = StringUtils.parseInt(wReader.get("StationID"));
////                wBFCMessage.StationNo = StringUtils.parseString(wReader.get("StationNo"));
////                wBFCMessage.Title = StringUtils.parseString(wReader.get("Title"));
////                wBFCMessage.Type = StringUtils.parseInt(wReader.get("Type"));
////                wBFCMessage.Active = StringUtils.parseInt(wReader.get("Active"));
////                wBFCMessage.ResponsorID = StringUtils.parseInt(wReader.get("ResponsorID"));
////                wBFCMessage.ModuleID = StringUtils.parseInt(wReader.get("ModuleID"));
////                wBFCMessage.StepID = StringUtils.parseInt(wReader.get("StepID"));
////                wBFCMessage.SendStatus = StringUtils.parseInt(wReader.get("SendStatus"));
////                wResult.add(wBFCMessage);
////
////            }
//        } catch (Exception e) {
//            wErrorCode.Result = ExceptionTypes.DBSQL.getValue();
//            logger.error("BFC_GetMessageList", e);
//        }
//        return wResult;
//    }

    public Map<Integer, Integer> BFC_GetMessageCount(
            BMSEmployee wLoginUser, int wResponsorID, List<Integer> wModuleID, OutResult<Integer> wErrorCode) {
        Map<Integer, Integer> wResult = new HashMap<Integer, Integer>();
        try {
            wResult.put(BFCMessageType.Task.getValue(), 0);
            wResult.put(BFCMessageType.Notify.getValue(), 0);
            List<Integer> wTaskActive = StringUtils.parseListArgs(BFCMessageStatus.Default.getValue(),
                    BFCMessageStatus.Sent.getValue(), BFCMessageStatus.Read.getValue());
            List<Integer> wNotifyActive = StringUtils.parseListArgs(BFCMessageStatus.Default.getValue(),
                    BFCMessageStatus.Sent.getValue());

            String wSQL = StringUtils.Format(
                    "SELECT  count(CASE WHEN t.Type=:wTaskType and t.Active IN ({1}) THEN t.ID ELSE NULL END) as " +
                            "TaskCount," +
                            " count(CASE WHEN t.Type=:wNotifyType and t.Active IN ({2}) THEN t.ID ELSE NULL END) as " +
                            "NotifyCount FROM {0}.bfc_message t WHERE 1=1 " +
                            " and ( :wResponsorID<= 0 or  t.ResponsorID  =:wResponsorID)   " +
                            " and ( :wModuleID ='''' or  t.ModuleID IN ({3}))   ", super.getDBName(),
                    StringUtils.Join(",", wTaskActive), StringUtils.Join(",", wNotifyActive),
                    wModuleID.size() > 0 ? StringUtils.Join(",", wModuleID) : "0");

            Map<String, Object> wParamMap = new HashMap<String, Object>();
            wParamMap.put("wTaskType", BFCMessageType.Task.getValue());
            wParamMap.put("wNotifyType", BFCMessageType.Notify.getValue());
            wParamMap.put("wModuleID", StringUtils.Join(",", wModuleID));
            wParamMap.put("wResponsorID", wResponsorID);

            Pagination wPagination = Pagination.getMaxSize(this.Clazz);
            List<Map<String, Object>> wQueryResult = super.queryForList(wSQL, wParamMap, Pagination.getMaxSize(this.Clazz),
                    super.TableName, super.GetCutTableName(wPagination.IsDesc()));

            for (Map<String, Object> wReader : wQueryResult) {
                wResult.put(BFCMessageType.Task.getValue(), StringUtils.parseInt(wReader.get("TaskCount")));
                wResult.put(BFCMessageType.Notify.getValue(), StringUtils.parseInt(wReader.get("NotifyCount")));
            }

        } catch (Exception e) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("BFC_GetMessageCount", e);
        }

        return wResult;
    }

    public List<BFCMessage> BFC_GetMessageList(
            BMSEmployee wLoginUser, int wResponsorID, int wStationID,  int wType,
            List<Integer> wModuleID, int wMessageID, int wStepID, List<Integer> wActive, int wSendStatus, int wShiftID,
            Calendar wStartTime, Calendar wEndTime, Pagination wPagination, OutResult<Integer> wErrorCode) {
        return this.BFC_GetMessageList(wLoginUser, wResponsorID, wStationID,  wType,
                wModuleID, StringUtils.parseListArgs(wMessageID), wActive, wSendStatus, wShiftID,
                wStartTime, wEndTime, wStepID, wPagination, wErrorCode);
        //
        // List<BFCMessage> wResult = new ArrayList<BFCMessage>();

//        try {
//
//            if (wStationNo == null) {
//                wStationNo = "";
//            }
//
//            Calendar wBaseTime = Calendar.getInstance();
//            wBaseTime.set(2000, 0, 1, 0, 0, 0);
//            if (wStartTime == null) {
//                wStartTime = wBaseTime;
//            }
//            if (wEndTime == null) {
//                wEndTime = wBaseTime;
//            }
//
////            Calendar wBaseTime = CalendarTool.getCalendar(2000, 1, 1);
//            if (wStartTime.compareTo(wBaseTime) < 0) {
//                wStartTime = wBaseTime;
//            }
//            if (wEndTime.compareTo(wBaseTime) < 0) {
//                wEndTime = wBaseTime;
//            }
//            if (wStartTime.compareTo(wEndTime) > 0) {
//                return wResult;
//            }
//
//            if (wActive == null) {
//                wActive = new ArrayList<Integer>();
//            }
//            if (wModuleID == null) {
//                wModuleID = new ArrayList<Integer>();
//            }
//
//            wActive.removeIf(p -> p < 0);
//
//            wModuleID.removeIf(p -> p <= 0);
//
//            String wSQL = StringUtils.Format("SELECT  * FROM {0}.bfc_message t WHERE 1=1 " +
//                            " and ( :wCompanyID<= 0 or  t.CompanyID  =:wCompanyID)   " +
//                            " and ( :wResponsorID<= 0 or  t.ResponsorID  =:wResponsorID)   " +
//                            " and ( :wStationID<= 0 or  t.StationID  =:wStationID)   " +
//                            " and ( :wStationNo is null or :wStationNo ='''' or  t.StationNo  =  :wStationNo )  " +
//                            " and ( :wType<= 0 or  t.Type  =:wType)   " +
//                            " and ( :wStepID<= 0 or  t.StepID  =:wStepID)   " +
//                            " and ( :wModuleID ='''' or  t.ModuleID  IN ({2}))   " +
//                            " and ( :wMessageID<= 0 or  t.MessageID  =:wMessageID)   " +
//                            " and ( :wActive ='''' or  t.Active  IN ({1}) )   " +
//                            " and ( :wSendStatus<= 0 or (:wSendStatus & t.SendStatus) = 0 )   " +
//                            " and ( :wShiftID <= 0 or  t.ShiftID   = :wShiftID)   " +
//                            " and ( :wStartTime <=str_to_date(''2010-01-01'', ''%Y-%m-%d'')  or :wStartTime <=  t" +
//                            ".EditTime )" +
//                            " and ( :wEndTime <=str_to_date(''2010-01-01'', ''%Y-%m-%d'')  or :wEndTime >=  t" +
//                            ".CreateTime ) ",
//                    super.getDBName(), wActive.size() > 0 ? StringUtils.Join(",", wActive) : "0",
//                    wModuleID.size() > 0 ? StringUtils.Join(",", wModuleID) : "0");
//
//            Map<String, Object> wParamMap = new HashMap<String, Object>();
//
//            wParamMap.put("wCompanyID", -1);
//            wParamMap.put("wResponsorID", wResponsorID);
//            wParamMap.put("wStationNo", wStationNo);
//            wParamMap.put("wStationID", wStationID);
//            wParamMap.put("wStepID", wStepID);
//            wParamMap.put("wType", wType);
//            wParamMap.put("wModuleID", StringUtils.Join(",", wModuleID));
//            wParamMap.put("wMessageID", wMessageID);
//            wParamMap.put("wSendStatus", wSendStatus);
//            wParamMap.put("wActive", StringUtils.Join(",", wActive));
//            wParamMap.put("wShiftID", wShiftID);
//            wParamMap.put("wStartTime", wStartTime);
//            wParamMap.put("wEndTime", wEndTime);
//
//            wSQL = this.DMLChange(wSQL);
//
//            List<Map<String, Object>> wQueryResult = super.queryForList(wSQL, wParamMap, wPagination);
//            // wReader\[\"(\w+)\"\]
//            for (Map<String, Object> wReader : wQueryResult) {
//
//                BFCMessage wBFCMessage = new BFCMessage();
//                wBFCMessage.ID = StringUtils.parseInt(wReader.get("ID"));
//                wBFCMessage.CreateTime = StringUtils.parseCalendar(wReader.get("CreateTime"));
//                wBFCMessage.CompanyID = StringUtils.parseInt(wReader.get("CompanyID"));
//                wBFCMessage.EditTime = StringUtils.parseCalendar(wReader.get("EditTime"));
//                wBFCMessage.MessageID = StringUtils.parseInt(wReader.get("MessageID"));
//                wBFCMessage.MessageText = StringUtils.parseString(wReader.get("MessageText"));
//                wBFCMessage.StationID = StringUtils.parseInt(wReader.get("StationID"));
//                wBFCMessage.StationNo = StringUtils.parseString(wReader.get("StationNo"));
//                wBFCMessage.Title = StringUtils.parseString(wReader.get("Title"));
//                wBFCMessage.Type = StringUtils.parseInt(wReader.get("Type"));
//                wBFCMessage.Active = StringUtils.parseInt(wReader.get("Active"));
//                wBFCMessage.ResponsorID = StringUtils.parseInt(wReader.get("ResponsorID"));
//                wBFCMessage.ModuleID = StringUtils.parseInt(wReader.get("ModuleID"));
//                wBFCMessage.StepID = StringUtils.parseInt(wReader.get("StepID"));
//                wBFCMessage.SendStatus = StringUtils.parseInt(wReader.get("SendStatus"));
//                wResult.add(wBFCMessage);
//
//            }
//        } catch (Exception e) {
//            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
//            logger.error("BFC_GetMessageList", e);
//        }
        // return wResult;
    }

    public List<BFCMessage> BFC_GetMessageList(
            BMSEmployee wLoginUser, int wResponsorID, int wType, List<Integer> wModuleID, int wMessageID, int wStepID,
            List<Integer> wActive, int wSendStatus, int wShiftID, Calendar wStartTime, Calendar wEndTime,
            Pagination wPagination, OutResult<Integer> wErrorCode) {
        List<BFCMessage> wResult = new ArrayList<BFCMessage>();
        try {
            wErrorCode.set(0);
            wResult = BFC_GetMessageList(wLoginUser, wResponsorID, -1,   wType, wModuleID, wMessageID, wStepID,
                    wActive, wSendStatus, wShiftID, wStartTime, wEndTime, wPagination, wErrorCode);

        } catch (Exception e) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("BFC_GetMessageList", e);
        }
        return wResult;
    }

    public List<BFCMessage> BFC_GetMessageList(
            BMSEmployee wLoginUser, int wResponsorID, int wType, List<Integer> wModuleID, List<Integer> wMessageIDList,
            List<Integer> wActive, int wSendStatus, int wShiftID, Calendar wStartTime, Calendar wEndTime, int wStepID,
            Pagination wPagination, OutResult<Integer> wErrorCode) {
        List<BFCMessage> wResult = new ArrayList<BFCMessage>();
        try {
            wErrorCode.set(0);
            wResult = BFC_GetMessageList(wLoginUser, wResponsorID, -1,  wType, wModuleID, wMessageIDList, wActive,
                    wSendStatus, wShiftID, wStartTime, wEndTime, wStepID, wPagination, wErrorCode);

        } catch (Exception e) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("BFC_GetMessageList", e);
        }
        return wResult;
    }

    /**
     * 查询待办消息
     */
    public List<BFCMessage> BFC_GetUndoMessageList(
            BMSEmployee wLoginUser, int wResponsorID, List<Integer> wModuleID, int wMessageID, int wShiftID,
            Calendar wStartTime, Calendar wEndTime, Pagination wPagination, OutResult<Integer> wErrorCode) {
        List<BFCMessage> wResult = new ArrayList<BFCMessage>();
        try {
            wErrorCode.set(0);
            wResult = BFC_GetMessageList(wLoginUser, wResponsorID, (Integer) BFCMessageType.Task.getValue(), wModuleID,
                    wMessageID, -1, StringUtils.parseListArgs((Integer) BFCMessageStatus.Default.getValue(),
                            (Integer) BFCMessageStatus.Sent.getValue(), (Integer) BFCMessageStatus.Read.getValue()), -1,
                    wShiftID, wStartTime, wEndTime, wPagination, wErrorCode);

        } catch (Exception e) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("BFC_GetUndoMessageList", e);
        }
        return wResult;
    }

    /**
     * 查询已办消息
     */
    public List<BFCMessage> BFC_GetDoneMessageList(
            BMSEmployee wLoginUser, int wResponsorID, List<Integer> wModuleID, int wMessageID, int wShiftID,
            Calendar wStartTime, Calendar wEndTime, Pagination wPagination, OutResult<Integer> wErrorCode) {
        List<BFCMessage> wResult = new ArrayList<BFCMessage>();
        try {
            wErrorCode.set(0);
            wResult = BFC_GetMessageList(wLoginUser, wResponsorID, (Integer) BFCMessageType.Task.getValue(), wModuleID,
                    wMessageID, -1, StringUtils.parseListArgs((Integer) BFCMessageStatus.Finished.getValue()), -1,
                    wShiftID, wStartTime, wEndTime, wPagination, wErrorCode);

        } catch (Exception e) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("BFC_GetUndoMessageList", e);
        }
        return wResult;
    }


    public List<BFCMessage> BFC_GetMessageList(
            BMSEmployee wLoginUser, int wResponsorID, int wType, List<Integer> wModuleID, int wMessageID,
            List<Integer> wActive, int wSendStatus, Pagination wPagination, OutResult<Integer> wErrorCode) {
        List<BFCMessage> wResult = new ArrayList<BFCMessage>();
        try {
            wErrorCode.set(0);
            Calendar wBaseTime = CalendarTool.getCalendar(2000, 1, 1);
            wResult = BFC_GetMessageList(wLoginUser, wResponsorID, wType, wModuleID, wMessageID, -1, wActive,
                    wSendStatus, -1, wBaseTime, wBaseTime, wPagination, wErrorCode);
        } catch (Exception e) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("BFC_GetMessageList", e);
        }
        return wResult;
    }

    /**
     * 查询待办任务数
     * <p>
     * :param wCompanyID :param wResponsorID :param wShiftID :return
     */
    public Map<Integer, Integer> BFC_GetUndoMessageCount(
            BMSEmployee wLoginUser, int wResponsorID, int wShiftID, OutResult<Integer> wErrorCode) {
        Map<Integer, Integer> wResult = new HashMap<Integer, Integer>();

        try {

            Calendar wBaseTime = CalendarTool.getCalendar(2000, 1, 1);
            List<BFCMessage> wBFCMessageList = BFC_GetUndoMessageList(wLoginUser, wResponsorID, null, -1, wShiftID,
                    wBaseTime, wBaseTime, Pagination.getMaxSize(), wErrorCode);

            Map<Integer, Long> wGroupResult = wBFCMessageList.stream().collect(
                    Collectors.groupingBy(BFCMessage::getModuleID, Collectors.counting()));

            for (int wModuleID : wGroupResult.keySet()) {
                wResult.put(wModuleID, wGroupResult.get(wModuleID).intValue());
            }

        } catch (Exception e) {

            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("BFC_GetUndoMessageCount", e);
        }
        return wResult;
    }

    public void BFC_UpdateMessage(BMSEmployee wLoginUser, BFCMessage wBFCMessage, OutResult<Integer> wErrorCode) {

        try {

            if (wBFCMessage == null) {
                return;
            }
            if (wBFCMessage.Title == null) {
                wBFCMessage.Title = "";
            }
            if (wBFCMessage.MessageText == null) {
                wBFCMessage.MessageText = "";
            }

            String wSQL = "";
            Map<String, Object> wParamMap = new HashMap<String, Object>();
            if (wBFCMessage.ID <= 0) {

                wSQL = StringUtils.Format("  INSERT INTO  {0} ( ResponsorID, Type, " +
                                "MessageText, Title, CreateTime, Active, EditTime, ModuleID, " +
                                "MessageID, StationID,  CompanyID, ShiftID,StepID,SendStatus) VALUES " +
                                "( :wResponsorID , :wType , :wMessageText , " +
                                ":wTitle , now() , 0 ,  now() , :wModuleID , :wMessageID , " +
                                ":wStationID ,  :wCompanyID , :wShiftID,:wStepID,:wSendStatus );",
                        this.getTableName(Calendar.getInstance()));
                if (wBFCMessage.ShiftID <= 0) {
                    wBFCMessage.ShiftID = CalendarTool.getShiftID(Calendar.getInstance());
                }
            } else {
                wSQL = StringUtils.Format(
                        "UPDATE {0} SET    EditTime = now() , Active = :wActive,SendStatus=:wSendStatus" +
                                "  WHERE ID>0  and Active != :wActiveF   and  :wID =ID  ;", this.getTableName(wBFCMessage.CreateTime));
            }

            wSQL = this.DMLChange(wSQL);
            wParamMap.clear();

            if (wBFCMessage.SendStatus == 0b111 && wBFCMessage.Active <   BFCMessageStatus.Sent.getValue()) {
                wBFCMessage.Active = BFCMessageStatus.Sent.getValue();
            }
            wParamMap.put("wID", wBFCMessage.ID);
            wParamMap.put("wResponsorID", wBFCMessage.ResponsorID);
            wParamMap.put("wType", wBFCMessage.Type);
            wParamMap.put("wMessageText", wBFCMessage.MessageText);
            wParamMap.put("wTitle", wBFCMessage.Title);
            wParamMap.put("wModuleID", wBFCMessage.ModuleID);
            wParamMap.put("wMessageID", wBFCMessage.MessageID);
            wParamMap.put("wStationID", wBFCMessage.StationID);
            wParamMap.put("wCompanyID", wBFCMessage.CompanyID);
            wParamMap.put("wShiftID", wBFCMessage.ShiftID);
            wParamMap.put("wActive", wBFCMessage.Active);
            wParamMap.put("wActiveF", (Integer) BFCMessageStatus.Finished.getValue());
            wParamMap.put("wStepID", wBFCMessage.StepID);
            wParamMap.put("wSendStatus", wBFCMessage.SendStatus);

            if (wBFCMessage.ID <= 0) {
                if (CutTableMode != DBCutTableModes.Default)
                    this.CreateTable();
                wBFCMessage.ID =  super.insert(wSQL, wParamMap);

            } else {
                super.update(wSQL, wParamMap);
            }

        } catch (Exception e) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("BFC_UpdateMessage", e);
        }
    }

    public void BFC_SendMessage(
            BMSEmployee wLoginUser, List<BFCMessage> wBFCMessage, int wSendStatus, OutResult<Integer> wErrorCode) {

        try {

            if (wBFCMessage == null || wBFCMessage.size() == 0 || wSendStatus <= 0) {
                return;
            }

            List<Integer> wMessageID = wBFCMessage.stream().map(p -> p.ID).collect(Collectors.toList());
            wMessageID.removeIf(p -> p <= 0);
            if (wMessageID.size() == 0) {
                return;
            }
            BFCMessageStatus wBFCMessageStatus = BFCMessageStatus.getEnumType(wSendStatus);
            if (wBFCMessageStatus == BFCMessageStatus.Default) {
                return;
            }


            Calendar wMin = wBFCMessage.stream().map(p -> p.CreateTime).min(Comparator.naturalOrder()).get();
            Calendar wMax = wBFCMessage.stream().map(p -> p.CreateTime).max(Comparator.naturalOrder()).get();


            String wSQL = StringUtils.Format(
                    "  update  {0}.{1} set SendStatus = (SendStatus|:SendStatus) where ID >0 AND ID IN ({1})",
                    super.getDBName(), StringUtils.Join(",", wMessageID));

            wSQL = this.DMLChange(wSQL);
            Map<String, Object> wParamMap = new HashMap<String, Object>();

            wParamMap.put("SendStatus", wSendStatus);
            super.update(wSQL, wParamMap, wMin, wMax);

        } catch (Exception e) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("BFC_SendMessage", e);
        }
    }

    public void BFC_InsertMessage(
            BMSEmployee wLoginUser, BFCMessage wBFCMessage, Map<Integer, String> wUserDic,
            OutResult<Integer> wErrorCode) {
        try {

            if (wBFCMessage == null || wUserDic == null || wUserDic.size() == 0) {
                return;
            }
            if (wBFCMessage.Title == null) {
                wBFCMessage.Title = "";
            }
            if (wBFCMessage.MessageText == null) {
                wBFCMessage.MessageText = "";
            }

            if (wBFCMessage.ShiftID <= 0) {
                wBFCMessage.ShiftID = CalendarTool.getShiftID(Calendar.getInstance());
            }

            if (wBFCMessage.SendStatus == 0b111 && wBFCMessage.Active < (Integer) BFCMessageStatus.Sent.getValue()) {
                wBFCMessage.Active = (Integer) BFCMessageStatus.Sent.getValue();
            }

            if (CutTableMode != DBCutTableModes.Default)
                this.CreateTable();

            StringBuilder wSB = new StringBuilder(StringUtils.Format(
                    "  INSERT INTO  {0} ( ResponsorID, Type, " +
                            "MessageText, Title, CreateTime, Active, EditTime, ModuleID, " +
                            "MessageID, StationID,  CompanyID, ShiftID,StepID,SendStatus) VALUES ",
                    this.getTableName(Calendar.getInstance())));

            int wIndex = 0;

            for (int wUserID : wUserDic.keySet()) {
                if (wIndex == 0) {
                    wSB.append(StringUtils.Format(
                            " ({0},{1},'{2}','{3}',now(),0,now(),{4},{5},{6},{7},{8},{9},{10}) ", wUserID,
                            wBFCMessage.Type, wBFCMessage.MessageText, wBFCMessage.Title, wBFCMessage.ModuleID,
                            wBFCMessage.MessageID, wBFCMessage.StationID,  wBFCMessage.CompanyID,
                            wBFCMessage.ShiftID, wBFCMessage.StepID, wBFCMessage.SendStatus));
                } else {
                    wSB.append(StringUtils.Format(
                            ",({0},{1},'{2}','{3}',now(),0,now(),{4},{5},{6},{7},{8},{9},{10})", wUserID,
                            wBFCMessage.Type, wBFCMessage.MessageText, wBFCMessage.Title, wBFCMessage.ModuleID,
                            wBFCMessage.MessageID, wBFCMessage.StationID,  wBFCMessage.CompanyID,
                            wBFCMessage.ShiftID, wBFCMessage.StepID, wBFCMessage.SendStatus));
                }
                wIndex++;
            }

            String wSQL = wSB.toString();

            wSQL = this.DMLChange(wSQL);

            super.update(wSQL);

            /*
             * if (wBFCMessage.Active == 0 && (wBFCMessage.SendStatus & 0b001) < 0) { //
             * 推送到中台 this.BFC_SendMessageList(wLoginUser, wBFCMessage, wUserDic.values(),
             * wErrorCode);
             *
             * }
             */

        } catch (

                Exception e) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("BFC_InsertMessage", e);
        }

    }

    public void BFC_ReceiveMessage(
            BMSEmployee wLoginUser, int wResponsorID, List<Integer> wMsgIDList, List<Integer> wStepID, int wModuleID,
            Calendar wMin, Calendar wMax, OutResult<Integer> wErrorCode) {

        try {
            this.BFC_HandleMessage(wLoginUser, wResponsorID, wMsgIDList, wStepID, wModuleID,
                    (Integer) BFCMessageType.Default.getValue(), (Integer) BFCMessageStatus.Read.getValue(), wMin, wMax,
                    wErrorCode);

        } catch (Exception e) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("BFC_ReceiveMessage", e);
        }
    }

    /**
     * :param wCompanyID :param wResponsorID 处理人 可以为空 :param wMsgIDList :param
     * wModuleID :param wType :param wStatus
     */
    public void BFC_HandleMessage(
            BMSEmployee wLoginUser, int wResponsorID, List<Integer> wMsgIDList, List<Integer> wStepID, int wModuleID,
            int wType, int wStatus, Calendar wMin, Calendar wMax, OutResult<Integer> wErrorCode) {
        try {

            if (wMsgIDList == null || wMsgIDList.size() < 1) {
                return;
            }
            if (wModuleID < 0) {
                return;
            }
            if (wStatus <= 0) {
                return;
            }

            if (wStepID == null) {
                wStepID = new ArrayList<Integer>();
            }

            String wSQL = "";
            Map<String, Object> wParamMap = new HashMap<String, Object>();
            // 判断是否存在
            wSQL = StringUtils.Format("UPDATE {0}.bfc_message SET  EditTime = now() , Active = :wActive  WHERE ID>0" +
                            "  AND   ( Active < :wActive or :wActive >= :wActiveF  ) and   Active != :wActiveF " +
                            " and  ModuleID =:wModuleID and ( :wType<=0 or Type=:wType)" +
                            "	and ( :wStepID =''''  or  StepID IN ({1}) ) " +
                            " and ( :wResponsorID<=0 or ResponsorID=:wResponsorID) and  MessageID  IN( {2} )  ;",
                    super.getDBName(), wStepID.size() > 0 ? StringUtils.Join(",", wStepID) : "0",
                    StringUtils.Join(",", wMsgIDList));

            wSQL = this.DMLChange(wSQL);
            wParamMap.put("wResponsorID", wResponsorID);
            wParamMap.put("wModuleID", wModuleID);
            wParamMap.put("wActiveF", (Integer) BFCMessageStatus.Finished.getValue());
            wParamMap.put("wActive", wStatus);
            wParamMap.put("wType", wType);
            wParamMap.put("wStepID", StringUtils.Join(",", wStepID));

            super.update(wSQL, wParamMap, wMin, wMax);

        } catch (Exception e) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("BFC_HandleMessage", e);
        }
    }

    /**
     * :param wCompanyID :param wResponsorID 处理人 可以为空 :param wMsgIDList :param
     * wModuleID :param wType :param wStatus
     */
    public void BFC_ForwardMessage(
            BMSEmployee wLoginUser, int wResponsorID, List<Integer> wForwarderList, int wModuleID, int wMessageID,
            int wStepID, OutResult<Integer> wErrorCode) {

        try {

            if (wForwarderList == null || wForwarderList.size() < 1) {
                return;
            }
            if (wModuleID < 0) {
                return;
            }
            if (wStepID <= 0) {
                return;
            }
            if (wMessageID <= 0) {
                return;
            }
            if (wResponsorID <= 0) {
                return;
            }

            BMSEmployee wResponsor = BMSEmployeeDAO.getInstance().BMS_QueryEmployeeByID(wLoginUser, wResponsorID,
                    wErrorCode);

            // 获取此任务转发人在此步骤最后一条代办消息
            List<BFCMessage> wBFCMessageList = this.BFC_GetMessageList(wLoginUser, wResponsorID, -1,
                    (Integer) BFCMessageType.Task.getValue(), StringUtils.parseListArgs(wModuleID), wMessageID, wStepID,
                    null, -1, -1, CalendarTool.getCalendar(2000, 1, 1), CalendarTool.getCalendar(2000, 1, 1),
                    Pagination.getDefault(), wErrorCode);

            if (wBFCMessageList.size() == 0) {
                return;
            }

            BFCMessage wBFCMessage = wBFCMessageList.get(wBFCMessageList.size() - 1);

            wBFCMessage.Active = (Integer) BFCMessageStatus.Finished.getValue();

            BFCMessage wBFCMessageF = null;
            for (int wForwarder : wForwarderList) {
                wBFCMessageF = CloneTool.Clone(wBFCMessage, BFCMessage.class);
                wBFCMessageF.ID = 0;
                wBFCMessageF.Active = (Integer) BFCMessageStatus.Default.getValue();
                wBFCMessageF.MessageText += StringUtils.Format("\n 转发人：{0}", wResponsor.Name);
                wBFCMessageF.ResponsorID = wForwarder;

                this.BFC_UpdateMessage(wLoginUser, wBFCMessageF, wErrorCode);

                //转发消息推送
                if (wBFCMessageF.ID > 0) {
                    BFCServiceImpl.ToSendList.offerLast(wBFCMessageF);
                }
            }

            this.BFC_UpdateMessage(wLoginUser, wBFCMessage, wErrorCode);


        } catch (Exception e) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("BFC_ForwardMessage", e);
        }
    }

    /**
     * :param wCompanyID :param wResponsorID 处理人 可以为空 :param wMsgIDList :param
     * wModuleID :param wType :param wStatus
     */
    public void BFC_HandleMessageByIDList(
            BMSEmployee wLoginUser, List<Integer> wMsgIDList, int wStatus, int wSendStatus, Calendar wMin, Calendar wMax,
            OutResult<Integer> wErrorCode) {

        try {

            if (wMsgIDList == null || wMsgIDList.size() < 1) {
                return;
            }
            wMsgIDList.removeIf(p -> p <= 0);

            if (wMsgIDList.size() < 1) {
                return;
            }

            if (wStatus <= 0) {
                return;
            }

            String wSQL = "";
            Map<String, Object> wParamMap = new HashMap<String, Object>();

            if (wStatus == (Integer) BFCMessageStatus.Sent.getValue()) {
                wSQL = StringUtils.Format("UPDATE {0}.bfc_message SET  EditTime = now() , " +
                                " SendStatus= :wSendStatus|SendStatus WHERE ID>0  and ID  IN( {1} )  ;",
                        super.getDBName(), StringUtils.Join(",", wMsgIDList));
                wParamMap.put("wSendStatus", wSendStatus);
                super.update(wSQL, wParamMap);

                wSQL = StringUtils.Format("UPDATE {0}.bfc_message SET  EditTime = now() , " +
                                " Active= :wActive  WHERE ID>0 and Active=0 and SendStatus=:wSendStatus    ;",
                        super.getDBName(), StringUtils.Join(",", wMsgIDList));
                wSendStatus = 0b111;

                wParamMap.clear();
                wParamMap.put("wSendStatus", wSendStatus);
                wParamMap.put("wActive", (Integer) BFCMessageStatus.Sent.getValue());
            } else {
                // 判断是否存在
                wSQL = StringUtils.Format("UPDATE {0}.bfc_message SET  EditTime = now() , Active = :wActive " +
                                "  WHERE  ID>0 AND Active < :wActive and   Active != :wActiveF and ID  IN( {1} )  ;",
                        super.getDBName(), StringUtils.Join(",", wMsgIDList));
                wParamMap.put("wActive", wStatus);
                wParamMap.put("wActiveF", (Integer) BFCMessageStatus.Finished.getValue());
            }

            wSQL = this.DMLChange(wSQL);

            super.update(wSQL, wParamMap, wMin, wMax);

        } catch (Exception e) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("BFC_HandleMessageByIDList", e);
        }
    }

    public void BFC_HandleTaskMessage(
            BMSEmployee wLoginUser, int wResponsorID, List<Integer> wMessageIDList, int wModuleID, int wStepID,
            int wStatus, int wAuto, Calendar wMin, Calendar wMax, OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);
        try {

            if (wMessageIDList == null || wMessageIDList.size() < 1) {
                return;
            }
            if (wModuleID < 0) {
                return;
            }
            if (wStatus <= 0) {
                return;
            }

            String wSQL = "";
            Map<String, Object> wParamMap = new HashMap<String, Object>();
            // 判断是否存在
            if (wStatus == (Integer) BFCMessageStatus.Finished.getValue() && wAuto > 0 &&
                    (wResponsorID > 0 || wResponsorID == BaseUser.SysAdmin.ID)) {

                wSQL = StringUtils.Format(
                        "UPDATE {0}.bfc_message SET  EditTime = now() , Active = :wActiveC  WHERE ID>0 " +
                                " and  ModuleID =:wModuleID  AND  StepID =:wStepID   and( :wType<=0 or Type=:wType) " +
                                " and  ResponsorID !=:wResponsorID  " +
                                " and   MessageID IN ( {1} ) and   Active != :wActiveF  and Active IN ({2})  ;",
                        super.getDBName(), StringUtils.Join(",", wMessageIDList),
                        StringUtils.Join(",", (Integer) BFCMessageStatus.Default.getValue(),
                                (Integer) BFCMessageStatus.Read.getValue(),
                                (Integer) BFCMessageStatus.Sent.getValue()));

                wParamMap.put("wResponsorID", wResponsorID);
                wParamMap.put("wModuleID", wModuleID);
                wParamMap.put("wActiveC", (Integer) BFCMessageStatus.Close.getValue());
                wParamMap.put("wActiveF", (Integer) BFCMessageStatus.Finished.getValue());
                wParamMap.put("wType", (Integer) BFCMessageType.Task.getValue());
                wParamMap.put("wStepID", wStepID);
                wSQL = this.DMLChange(wSQL);
                super.update(wSQL, wParamMap,wMin,wMax);

                wSQL = StringUtils.Format(
                        "UPDATE {0}.bfc_message SET  EditTime = now() , Active = :wActive  WHERE ID>0 " +
                                " and  ModuleID =:wModuleID  AND  StepID =:wStepID and( :wType<=0 or Type=:wType) " +
                                " and  ResponsorID =:wResponsorID " +
                                " and  MessageID  IN( {1} ) and Active != :wActiveF  and Active IN ({2}) ;",
                        super.getDBName(), StringUtils.Join(",", wMessageIDList),
                        StringUtils.Join(",", (Integer) BFCMessageStatus.Default.getValue(),
                                (Integer) BFCMessageStatus.Read.getValue(),
                                (Integer) BFCMessageStatus.Sent.getValue()));

            } else {
                wSQL = StringUtils.Format(
                        "UPDATE {0}.bfc_message SET  EditTime = now() , Active = :wActive  WHERE ID>0 " +
                                " and ModuleID =:wModuleID  AND  StepID =:wStepID  and (:wType<=0 or Type=:wType) " +
                                " and (:wResponsorID<=0 or ResponsorID=:wResponsorID) " +
                                " and MessageID  IN( {1} )  and Active != :wActiveF  and Active IN ({2}) ;",
                        super.getDBName(), StringUtils.Join(",", wMessageIDList),
                        StringUtils.Join(",", (Integer) BFCMessageStatus.Default.getValue(),
                                (Integer) BFCMessageStatus.Read.getValue(),
                                (Integer) BFCMessageStatus.Sent.getValue()));

            }

            wSQL = this.DMLChange(wSQL);
            wParamMap.clear();
            wParamMap.put("wResponsorID", wResponsorID);
            wParamMap.put("wStepID", wStepID);
            wParamMap.put("wModuleID", wModuleID);
            wParamMap.put("wActive", wStatus);
            wParamMap.put("wActiveF", BFCMessageStatus.Finished.getValue());
            wParamMap.put("wType", BFCMessageType.Task.getValue());

            super.update(wSQL, wParamMap, wMin, wMax);

        } catch (Exception e) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("BFC_HandleTaskMessage", e);
        }
    }

    public void BFC_CloseTask(
            BMSEmployee wLoginUser, int wUserID, int wTaskID, int wModuleID, Calendar wMin, Calendar wMax, OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);
        try {

            if (wModuleID < 0) {
                return;
            }
            if (wTaskID <= 0) {
                return;
            }

            String wSQL = "";
            Map<String, Object> wParamMap = new HashMap<String, Object>();
            // 判断是否存在
            wSQL = StringUtils.Format("UPDATE {0}.bfc_message SET  EditTime = now() , Active = :wActive  WHERE ID>0 " +
                            " and  ModuleID =:wModuleID  and Type=:wType " +
                            " and MessageID=:wMessageID  and Active IN  ({1}) AND ResponsorID!=:wResponsorID ;",
                    super.getDBName(),
                    StringUtils.Join(",", BFCMessageStatus.Default.getValue(), BFCMessageStatus.Read.getValue(),
                            BFCMessageStatus.Sent.getValue()));

            wSQL = this.DMLChange(wSQL);
            wParamMap.put("wModuleID", wModuleID);
            wParamMap.put("wActive", (Integer) BFCMessageStatus.Close.getValue());
            wParamMap.put("wMessageID", wTaskID);
            wParamMap.put("wResponsorID", wUserID);
            wParamMap.put("wType", (Integer) BFCMessageType.Task.getValue());

            super.update(wSQL, wParamMap, wMin, wMax);

            wSQL = StringUtils.Format("UPDATE {0}.bfc_message SET  EditTime = now() , Active = :wActive  WHERE ID>0 " +
                            " and  ModuleID =:wModuleID  and Type=:wType and MessageID=:wMessageID  and Active IN " +
                            "({1})  AND ResponsorID = :wResponsorID ;",
                    super.getDBName(), StringUtils.Join(",", (Integer) BFCMessageStatus.Default.getValue(),
                            (Integer) BFCMessageStatus.Read.getValue(), (Integer) BFCMessageStatus.Sent.getValue()));
            wParamMap.put("wActive", (Integer) BFCMessageStatus.Finished.getValue());

            super.update(wSQL, wParamMap, wMin, wMax);

        } catch (Exception e) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("BFC_CloseTask", e);
        }
    }

    private BFCMessageDAO() {
        super("");
        this.Init();
    }

    public static BFCMessageDAO getInstance() {
        if (Instance == null) {
            Instance = new BFCMessageDAO();
        }
        return Instance;
    }


    /**
     * 判断消息是否存在
     */
    public boolean IsMessageExist(BMSEmployee wLoginUser, int wStepID, OutResult<Integer> wErrorCode) {
        boolean wResult = false;
        try {
            String wSQL = StringUtils.Format(
                    "SELECT COUNT(*) Number FROM {0}.bfc_message WHERE stepid=:stepid and Active in (0,1,2);",
                    DBSchemas.Basic.getDBName());

            Map<String, Object> wParamMap = new HashMap<>();

            wParamMap.put("stepid", wStepID);

            wSQL = this.DMLChange(wSQL);

            List<Map<String, Object>> wQueryResult = this.queryForList(wSQL, wParamMap, this.TableName, this.GetCutTableName());

            for (Map<String, Object> wReader : wQueryResult) {
                int wNumber = StringUtils.parseInt(wReader.get("Number"));
                if (wNumber > 0)
                    wResult = true;
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    public BFCMessage BFC_GetMessageByID(BMSEmployee wLoginUser, int wID, OutResult<Integer> wErrorCode) {
        BFCMessage wResult = new BFCMessage();
        try {


            wResult= this.SelectPO(wLoginUser,wID,"",wErrorCode);

//
//            String wSQL = StringUtils.Format("SELECT  t.* FROM {0}.bfc_message t WHERE 1=1 " +
//                            " and ( :wID<= 0 or  t.ID  =:wID) ",
//                    super.getDBName());
//
//            Map<String, Object> wParamMap = new HashMap<String, Object>();
//
//            wParamMap.put("wID", wID);
//
//            wSQL = super.DMLChange(wSQL);
//            BFCMessage wBFCMessage = null;
//
//            List<Map<String, Object>> wQueryResult = super.queryForList(wSQL, wParamMap);
//            // wReader\[\"(\w+)\"\]
//            for (Map<String, Object> wReader : wQueryResult) {
//                wResult.ID = StringUtils.parseInt(wReader.get("ID"));
//                wResult.CreateTime = StringUtils.parseCalendar(wReader.get("CreateTime"));
//                wResult.CompanyID = StringUtils.parseInt(wReader.get("CompanyID"));
//                wResult.EditTime = StringUtils.parseCalendar(wReader.get("EditTime"));
//                wResult.MessageID = StringUtils.parseInt(wReader.get("MessageID"));
//                wResult.MessageText = StringUtils.parseString(wReader.get("MessageText"));
//                wResult.StationID = StringUtils.parseInt(wReader.get("StationID"));
//                wResult.StationNo = StringUtils.parseString(wReader.get("StationNo"));
//                wResult.Title = StringUtils.parseString(wReader.get("Title"));
//                wResult.Type = StringUtils.parseInt(wReader.get("Type"));
//                wResult.Active = StringUtils.parseInt(wReader.get("Active"));
//                wResult.ResponsorID = StringUtils.parseInt(wReader.get("ResponsorID"));
//                wResult.ModuleID = StringUtils.parseInt(wReader.get("ModuleID"));
//                wResult.StepID = StringUtils.parseInt(wReader.get("StepID"));
//                wResult.SendStatus = StringUtils.parseInt(wReader.get("SendStatus"));
//            }
        } catch (Exception e) {
            wErrorCode.Result = ExceptionTypes.DBSQL.getValue();
            logger.error("BFC_GetMessageByID", e);
        }
        return wResult;
    }

}
